/**
 * 审批
 */
function ApprovalModule(ui,opnCtl,engine) {
	this.base=Module;
	this.base(ui,engine);
	this.opnCtl=opnCtl;

	if(opnCtl)
		this.state=false;
	else
		this.state=true;

	this.load();
}
ApprovalModule.prototype = new Module();


ApprovalModule.prototype.isOk=function(){
	return (this.state==null||typeof(this.state)=='undefined')?false:this.state;
};

ApprovalModule.prototype.alert=function(){
	dialog.alert("请填写意见！");
};

ApprovalModule.prototype.load=function(){
	var workId=this.engine.workId;
	var curAct=this.engine.currentActivity;
	var todoId=this.engine.todoId;
	var approvalModule=this;
	$(".opinion-area").each(function(index,element){
		var activity=$(element).attr("activity");
		$.ajax({
			url : ctxPath + '/jbpmService/option/actOption',
			type : 'post',
			dataType : 'text',
			data : 'workId='+workId+'&activity=' + activity,
			success : function(data) {
				console.log(activity+"的意见："+data);
				if(data&&data!=null&&data!="null"){
					var opns=Util.strToObj(data);
					for(var i=0;i<opns.length;i++){
						opn=opns[i];
						if(todoId==opn[6])//有签署的意见
							approvalModule.state=true;
						var html=approvalModule.newOpnEle(opn[4],opn[1],opn[3],null,opn[2]);
						$(element).find(".opinion-list").append(html);
					}
				}
			}
		});

		if(approvalModule.opnCtl&&curAct&&curAct==activity){//只有当前环节的审批意见才能填写
			var title=$(element).find(".opinion-title");
			var titleVal=title.text()+"意见";
			title.append('<span class="ion-edit" style="margin-left: 15px"></span>');

			$(element).find(".opinion-title>span").click(function(){
				var opnForm='<textarea class="form-control"></textarea>';
				approvalModule.opinionText=$(opnForm);
				approvalModule.opinionArea=element;
				dialog.modal1({
					title : titleVal||"填写意见",
					sourceType : "local",// local 表示内容来源本地, remote 表示内容从网络请求获取
					htmlContent : approvalModule.opinionText,// 若sourceType=local时，htmlContent不能为空
					btnFunction : function() {
						approvalModule.save();
					}
				});
			});
		}
	});
};

ApprovalModule.prototype.save=function(){
	var approvalModule=this;
	var opnV=this.opinionText.val();
	var workId=this.engine.workId;
	var act=this.engine.currentActivity;
	$.ajax({
		url : ctxPath + '/jbpmService/option/save',
		type : 'post',
		dataType : 'text',
		data : 'opinion='+opnV+'&workId='+workId+'&activity='+act,
		success : function(data) {
			var resp = util.strToObj(data);
			if(resp.code=="success"){
				approvalModule.state=true;
				var opinion=resp.data.opinion;
				var html=approvalModule.newOpnEle(opinion.opinionId,opinion.opinionContent,opinion.sigerName,null, opinion.signTime);
				$(approvalModule.opinionArea).find("#"+opinion.opinionId).remove();
				$(approvalModule.opinionArea).find(".opinion-list").append(html);
			}
			dialog.alert(resp.desc);
		}
	});
};

ApprovalModule.prototype.newOpnEle=function(id,opn,sigName,sigImg,sigTime){
	var content='<div class="opinion-content">'+opn+'</div>';
	var signature='<div class="opinion-signature">'+sigName+'</div>';
	if(sigImg!=null&&sigImg.length>0)
		signature='<div class="opinion-signature"><img src="'+ctxPath+'/'+sigImg+'" /></div>';
	var time='<div class="opinion-time">'+sigTime+'</div>';
	var html='<div class="opinion-item" id="'+id+'">'+content+signature+time+'</div>';
	return html;
};

ApprovalModule.prototype.del=function(){

};